public  lcd_init
public  write_com
public  write_data
public  put_str
public  delay3ms
ss1      segment     code
rseg    ss1



lcdrs   bit     p2.5         //p3.5
lcdrw   bit     p2.6        //p2.1
lcden   bit     p2.7       //p3.4
cxpos   equ     21h
cypos   equ     22h

;-------------------------------------------------
;Name of Subprogram: lcd_init
;Function:Initialize LCD control unit
;input:None
;output:None
;Effects: The values of R0 and ACC
;-------------------------------------------------

lcd_init:
        
        mov     a,#01h
        call    write_com
        call    delay3ms
        
        mov     a,#38h
        call    write_com
        call    delay3ms

        
        mov     a,#0fh
        call    write_com
        call    delay3ms

        mov     a,#06h
        call    write_com
        call    delay3ms

         mov    a,#80h
        call    write_com
        call    delay3ms

        ret
;--------------------------------------------------




        
;-------------------------------------------------
;Name of Subprogram: write_com
;Function:Send command to the control unit of LCD
;input:A
;output:None
;Effects: The value of R0
;-------------------------------------------------

write_com:
        clr     lcdrs
        clr     lcdrw
        mov     p0,a
        call    delay3ms
        setb    lcden
        call    delay3ms
        clr     lcden
        ret
;-------------------------------------------------


;-------------------------------------------------
;Name of Subprogram: write_data
;Function:Send data to the control unit of LCD
;input:A
;output:None
;Effects: The value of R0
;-------------------------------------------------

write_data:
        setb    lcdrs
        clr     lcdrw
        mov     p0,a
        call    delay3ms
        setb    lcden
        call    delay3ms
        clr     lcden
        ret
;-------------------------------------------------


;-------------------------------------------------
;Name of Subprogram: lcd_read
;Function:Read data from the control unit of LCD
;input:None
;output:A
;Effects: The value of R0
;-------------------------------------------------

lcd_read:
        call    lcd_wait_idle
        mov     p0,#0ffh
        setb    lcdrs
        setb    lcdrw
        setb    lcden
        nop
        nop
        mov     a,p0
        clr     lcden
        ret
;-------------------------------------------------


;-------------------------------------------------
;Name of Subprogram: lcd_wait_idle
;Function:Busy check of the LCD control unit
;input:None
;output:None
;Effects: The value of R0
;-------------------------------------------------

lcd_wait_idle:
        push    acc         ;Generally,before 
                            ;reading and writing 
                            ;operations, the 7 bit
                            ;of SW of LCD control 
                            ;unit must be checked.
                            ;only when d7=0,the op
                            ;eration of reading and
                            ;writing would be accepted.      
        mov     p0,#0ffh
        clr     lcdrs
        setb    lcdrw
        setb    lcden
        mov     r0,#20

wtd_pa:                     ;D7=0 means that LCD control
        nop                 ;unit is idle, so quit from 
        nop                 ;the busy check.
        jnb     p0.7,wtd_lax
        djnz    r0,wtd_pa   ;D7=1 menas that LCD control
        nop                 ;unit is busy,so keep checking.
        nop                 ;After 100us, if D7 still equals
                            ;to 1 means that the LCD control
                            ;unit works uncorrectlly.Also 
                            ;quit from the busy check
     
wtd_lax:
        clr     lcden
        pop     acc
        ret    
;----------------------------------------------------    


;-----------------------------------------------------
;Name of Subprogram: put_str
;Function:At (cxpos,cypos) write string
;input:Dptr
;output:None
;Effects: The values of R0,ACC,DPTR
;-----------------------------------------------------

put_str:
        clr     a
        movc    a,@a+dptr
        jz      psr_lax     ;0 means the end of the string
        call    put_char    ;write one char
        call    char_cursor_next ;move the pozision 
                                 ;to the next
        inc     dptr        ;move the string point to
                            ;the next
        jmp     put_str     ;re-read string
psr_lax:
        ret
;------------------------------------------------------

;-----------------------------------------------------
;Name of Subprogram: put_char
;Function:At (cxpos,cypos) write char
;input:None
;output:None
;Effects: None
;-----------------------------------------------------
put_char:
        call    char_lcd_pos
        call    write_data
        ret
;-----------------------------------------------------

;-----------------------------------------------------
;Name of Subprogram: char_lcd_pos
;Function:Set the DDRAM address of pozision(cxpos,cypos)
;input:None
;output:None
;Effects: None
;-----------------------------------------------------

char_lcd_pos:
        push    acc
        anl     cxpos,#0fh
        anl     cypos,#01h
        mov     a,cypos
        cjne    a,#00h,lps_lay
        mov     a,cxpos
        jmp     lps_lax
lps_lay:
        mov     a,cxpos
        add     a,#40h
lps_lax:
        orl     a,#80h
        call    write_com
        pop     acc
        ret
;------------------------------------------------------


;-----------------------------------------------------
;Name of Subprogram: char_cursor_next
;Function:Set the char to the next avilable pozision
;input:None
;output:None
;Effects: The value of R0
;-----------------------------------------------------

char_cursor_next:
        inc     cxpos
        anl     cxpos,#0fh
        mov     r0,cxpos
        cjne    r0,#00h,csn_lax
        inc     cypos
        anl     cypos,#01h
csn_lax:
        ret
;----------------------------------------------------

;-------------------------------------------------
;Name of Subprogram: delay3ms
;Function:3 ms delay
;input:None
;output:None
;Effects: The values of R1 and R2
;-------------------------------------------------

delay3ms:
        push    1
        mov     r1,#15
dl3_pa:
        mov     r2,#100
        djnz    r2,$
        djnz    r1,dl3_pa
        pop     1
        ret

;------------------------------------------------

end


















